package com.eascs.ticket.ticket.controller;

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.util.ObjectArrayUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.ticket.entity.Ticket;
import com.eascs.ticket.ticket.form.TicketSearchForm;
import com.eascs.ticket.ticket.service.TicketVersionService;
import com.eascs.ticket.user.service.UserStructureService;
import com.eascs.web.exception.RedirectException;

@Controller
public class TicketSearchViewController extends BaseMasterViewController {
	@Inject
	private TicketSearchComponent ticketSearchComponent;

	@Inject
	private TicketVersionService ticketVersionService;

	@Inject
	private UserStructureService userStructureService;

	@RequestMapping("/ticket/view/search")
	public String search(TicketSearchForm ticketSearchForm, HttpServletResponse httpServletResponse) throws IOException {
		PaginationInfo paginationInfo = null;
		if ("excel".equals(ticketSearchForm.getAction())) {
			ticketSearchForm.setPageSize(Integer.MAX_VALUE);
			paginationInfo = ticketSearchComponent.search(ticketSearchForm);
			paginationInfo.getPageResult().setData(ObjectArrayUtil.buildObjectArrayList(paginationInfo.getPageResult().getData(), new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 21, 20, 19, 24 }));
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=ticket.xls");
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			exportExcel(httpServletResponse, paginationInfo);
			return null;
		} else {
			paginationInfo = ticketSearchComponent.search(ticketSearchForm);
		}
		// 使用工单号查找并找到只能一条则做跳转处理
		String keyWord = ticketSearchForm.getKeyWord();
		PageResult<Object[]> pageResult = paginationInfo.getPageResult();
		if (StringUtil.hasText(keyWord) && pageResult.getDbRowCount() == 1 && pageResult.getPageIndex() == 0 && keyWord.equals(pageResult.getData().get(0)[0])) {
			throw new RedirectException("/ticket/view/" + keyWord);
		}
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("ticketSearchForm", ticketSearchForm);

		return "/ticket/view/search";
	}

	private void exportExcel(HttpServletResponse httpServletResponse, PaginationInfo paginationInfo) throws IOException {
		ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), TicketSearchViewController.class.getResourceAsStream("/excel/search.xls"), new ExportExcelRowWriteHandlerImpl(paginationInfo));
	}

	@RequestMapping("/ticket/view/mywatcher")
	public String getMyWatcher(TicketSearchForm ticketSearchForm) {
		PaginationInfo paginationInfo = ticketSearchComponent.getMyWatcher(ticketSearchForm);
		ViewContext.put("paginationInfo", paginationInfo);
		return "/ticket/view/mywatcher";
	}

	@RequestMapping("/ticket/view/mydone")
	public String getMyDone(TicketSearchForm ticketSearchForm) {
		PaginationInfo paginationInfo = ticketSearchComponent.getMyDone(ticketSearchForm);
		ViewContext.put("paginationInfo", paginationInfo);
		return "/ticket/view/mywatcher";
	}

	@RequestMapping("/ticket/view/versions")
	public String viewVersions() {
		List<Ticket> versions = ticketVersionService.getAllNotCloseVersion();
		ViewContext.put("versions", versions);
		return "/ticket/view/versions";
	}

	@RequestMapping("/ticket/view/operators")
	public String viewOperators() {
		ViewContext.put("userStructureViews", this.userStructureService.getAllUserStructureViews());
		return "/ticket/view/operators";
	}
}
